package com.inossem.warehousestorage_inossem.activity.out_storage;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.outStorage.SaleOutStorageListAdapter;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStorageSaleListBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.outStorage.OutStorageSaleListRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

//免检入库
public class OutStorageSaleListActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView saleOutStorageList;

    private List<OutStorageSaleListBean.DataBean.SaleOutStorageListBean> allData;

    private SaleOutStorageListAdapter adapter;

    private String mSaleNumber;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_out_storage_sale_list);
        initView(OutStorageSaleListActivity.this);
        initData(OutStorageSaleListActivity.this);
    }

    @Override
    protected void onResumeImpl() {

    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        saleOutStorageList = (PullableListView) activity.findViewById(R.id.saleOutStorageList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.saleOutStorageListRefresh);
    }

    private void initData(Activity activity) {
        title.setText("销售出库");
        ActivityUtils.initBackAndFunction(activity, "出库", new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        saleOutStorageList.setPullUpFlag(false);
        saleOutStorageList.setPullDownFlag(false);
        resetData(activity);
//        setmScanListener((String scanResult, int length) -> {
//            mSaleNumber = QRCodeUtils.DecryptQRCode(scanResult);
//            getData(activity, mSaleNumber);
//        }, QRCodeConstant.TYPE_BATCH, QRCodeConstant.TYPE_MATERIEL_CODE, QRCodeConstant.TYPE_PURCHASE_ORDER, QRCodeConstant.TYPE_POSITION);
    }

    private void resetData(Context context) {
        mSaleNumber = "";
        allData = new ArrayList<>();
        addOutStorageSaleListData(context, allData, saleOutStorageList, true);
    }

    private void addOutStorageSaleListData(Context context, List<OutStorageSaleListBean.DataBean.SaleOutStorageListBean> saleOutStorageList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new SaleOutStorageListAdapter(context, saleOutStorageList, mSaleNumber, (String saleNumber) -> {
                String checkResult = check(saleNumber);
                if (checkResult == null) {
                    mSaleNumber = saleNumber;
                    getData(context, mSaleNumber);
                } else {
                    Utils.middleToast(context, checkResult);
                }
            });
            listView.setAdapter(adapter);
        }
    }

    private String check(String saleNumber) {
        String result = null;
        if (TextUtils.isEmpty(saleNumber)) {
            result = "销售单号不可以为空";
        }
        return result;
    }

    private void getData(Context context, String saleNumber) {
        OutStorageSaleListRequest.request(context, saleNumber, new GsonBeanListener<OutStorageSaleListBean>() {
            @Override
            public void onSuccess(OutStorageSaleListBean bean) {
                allData = bean.getData().getSaleOutStorageList();
                if (allData.isEmpty()) {
                    Utils.middleToast(context, "未查到数据");
                }
                addOutStorageSaleListData(context, ActivityUtils.getShowData(allData, 0), saleOutStorageList, true);
            }
        });
    }

    public interface OutStorageSaleListener {
        void onNoDoubleClickListener(String pickingNumber);
    }

}
